package cheqex.update.controllers;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;

import cheqex.dao.Withdrawals;
import cheqex.dao.WithdrawalsPeer;
import cheqex.dao.WithdrawalsPeer;
import cheqex.dao.WithdrawalsPeer;
import cheqex.dao.WithdrawalsPeer;


public class WithdrawalsController {
	private String date;
	private static Object[][] list = { };
	
	public WithdrawalsController(String userDate){
		this.date = userDate;
	}


	public static boolean addWithdrawals(String checkNumber, java.sql.Date date, int amount, String particular, int id){
		try{
			if(WithdrawalsPeer.addWithdrawals(checkNumber, amount, particular, date, id))
				return true;
			else
				return false;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean editWithdrawals(String checkNumber, int amount, String particular, java.sql.Date date){
		try{
			WithdrawalsPeer.editWithdrawals(checkNumber, amount, particular, date);
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteWithdrawals(String checkNumber, java.sql.Date date){
		try{
			WithdrawalsPeer.deleteWithdrawals(checkNumber, date);
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static int validateDataToBeAdded(String number, int amount, String particular){
		int errcheck_id=8;
		if(number.isEmpty()){
			if(Integer.toString(amount).isEmpty() || amount==-9999){
				if(particular.isEmpty())errcheck_id=3; //missing number & amount & particular
				else errcheck_id=2; //missing number & amount 
			}else if(particular.isEmpty()) errcheck_id=4; //missing number & particular
			else errcheck_id=1;//missing number
		}else if(Integer.toString(amount).isEmpty() || amount==-9999){
			if(particular.isEmpty()) errcheck_id = 5; //missing amount & particular
			else errcheck_id=6; //missing amount
		}else if(particular.isEmpty()) errcheck_id=7; //missing particular
		
		return errcheck_id;
	}
	
	public static boolean isNumeric(String input){ //checks if number
		try 
		{
			 char[] ch = input.toCharArray(); 
			 for(int i = 0; i < input.length();i++) {
		            if( Character.isDigit(ch[i]) ) 
		            	continue; 
		            else return false;
			 }
			 return true;
		}catch (NumberFormatException nfe) {
			nfe.printStackTrace();
			return false;
		}
	
	}
	
	
	public static boolean findCheck(String checkNumber, java.sql.Date date){
		try{
			if(WithdrawalsPeer.getCheck(checkNumber, date)!=null)
				return true;
			else return false;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static int getAmtCash(String key){
		try{
			List<Withdrawals> list = WithdrawalsPeer.getData(key);
			Withdrawals withdrawals = list.get(0);
			return withdrawals.getAmount();
		}catch (Exception e){
			e.printStackTrace();
			return -9999;
		}
	}
	
	public static String getParticularCash(String key){
		try{
			List<Withdrawals> list = WithdrawalsPeer.getData(key);
			Withdrawals withdrawals = list.get(0);
			return withdrawals.getParticulars();
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
}
